package Domain.UserClient;

import Business.FrameHandler;
import Domain.DataManager;
import Domain.stompProtocol.StompClient;
import Logger.ClientLogger;
import java.util.logging.Level;

public class UserClient {
        public static void main(String[] args) //throws InterruptedException
	{
            StompClient stompClient = null ;
            FrameHandler.getFrameHandler().initiateFrameHandler();
        
            try
            {
                stompClient = new StompClient("localhost",61635);
                DataManager.isConnected = true;
            }
            catch (Exception ex)
            {
                DataManager.isConnected = false;
                ClientLogger.write(Level.INFO,"no connection to stomp server");
                FrameHandler.getFrameHandler().showErrorMessage("no connection to stomp server, please try again later");
                System.exit(0);
            }

            DataManager dm = DataManager.getDataManager();
            ButtonsFunctions bf = ButtonsFunctions.getButtonsFunctions(stompClient, dm);
            SocketListener socketList = new SocketListener(bf, dm);
            try{
                while(DataManager.isConnected)
                {
                	socketList.processMessage(stompClient.getStompFrame());
                }
            }
          catch (Exception e) {
                ClientLogger.write(Level.INFO,"ERROR Connection DEAD!!!");
               //System.out.println("ERROR Connection DEAD!");
              //for killing the Welcome thread

        	 DataManager.isConnected = false;
                  dm.releaseAllKeys();
                //dm.setVectorForums(null);
               

              String error = "no connection to server, please try again later";
              FrameHandler.getFrameHandler().showErrorMessage(error);
              ClientLogger.write(Level.INFO,"ERROR Connection DEAD!!!");
              //System.out.println("ERROR Connection DEAD!");
              System.exit(0);

          }

        }

}


